#邮箱:448251593qq.com
#qq:448251593
#微信:448251593

import os
import gc

import time
#算法
def pi(places=100):
  # 3 + 3*(1/24) + 3*(1/24)*(9/80) + 3*(1/24)*(9/80)*(25/168)
  # The numerators 1, 9, 25, ... are given by (2x + 1) ^ 2
  # The denominators 24, 80, 168 are given by (16x^2 -24x + 8)
  extra = 8
  one = 10 ** (places+extra)
  t, c, n, na, d, da = 3*one, 3*one, 1, 0, 0, 24

  while t > 1:
    n, na, d, da = n+na, na+8, d+da, da+32
    t = t * n // d
    c += t
  
  return c // (10 ** extra)

#计算位数
def pi_t(n=1000):
    gc.collect()
    t1 = time.time()
    ret = pi(n)
    t2 = time.time()
    print('  ', (t2- t1), 'ms', 'pi=',ret)
#函数入口
if __name__ == '__main__':
  for i in (100, 500):
    print('\nCalc {} bits pi'.format(i))
    pi_t(i)

